package com.wenhua.service.impl;

import com.wenhua.mapper.DormitoryMapper;
import com.wenhua.mapper.StudentMapper;
import com.wenhua.pojo.Dormitory;
import com.wenhua.pojo.Student;
import com.wenhua.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/* 学生实现类 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private DormitoryMapper dormitoryMapper;

    @Override
    public List<Student> selectStudent(Integer currentPage, Integer pageSize, String selectString, String sort) {

        //首先计算偏移量
        Integer offset = (currentPage - 1) * pageSize;

        //调用mapper操控数据库查询
        List<Student> students = studentMapper.selectStudent(offset, pageSize, selectString, sort);

        return students;
    }
    /* 查询学生出入 */
    @Override
    public List<Student> selectStudentMove(Integer currentPage, Integer pageSize, String selectString, String sort) {

        //首先计算偏移量
        Integer offset = (currentPage - 1) * pageSize;

        //调用mapper查询
        List<Student> students = studentMapper.selectStudentMove(offset, pageSize, selectString, sort);

        return students;
    }

    //查询学生数量
    @Override
    public Integer selectStudentCount(String selectString) {

        return studentMapper.selectStudentCount(selectString);
    }
    //查询学生出入数量
    @Override
    public Integer selectStudentMoveCount(String selectString) {

        return studentMapper.selectStudentMoveCount(selectString);
    }

    //添加学生信息
    @Override
    public void addStudent(Student student) {
        Integer dormitoryId = student.getDormitoryId();
        //调用mapper
        studentMapper.addStudent(student);
        Integer studentId = student.getStudentId();

        System.out.println("学生->"+studentId);
        //直接添加学生到宿舍
        dormitoryMapper.addStudentDormitory(studentId,dormitoryId);

        System.out.println();
    }

    /* 通过id查询学生 */
    @Override
    public Student selectStudentById(Integer studentId) {
        return studentMapper.selectStudentById(studentId);
    }

        /* 更新学生信息 */
    @Override
    public void updateStudent(Student student) {
        //先更新学生表中的数据
        studentMapper.updateStudent(student);
        //然后更新宿舍表中的数据
        dormitoryMapper.updateStudentDormitory(student.getStudentId(),student.getDormitoryId());
        //ok
    }

        /* 删除学生信息 *//**/
    @Override
    public void deleteStudent(Integer id){
        ///
        studentMapper.deleteStudent(id);
    }

    /* 记录学生出入 */
    @Override
    public void studentMove(Integer id, String moveType){
        // 获取当前时间
        Date currentTime = new Date();
        Timestamp timestamp = new Timestamp(currentTime.getTime());
        /* 这样传入当前时间行吗? */
        studentMapper.studentMove(id,moveType,timestamp);
    }
}
